"""文件名必须是tasks，这样celery才能检测到"""
from celery.local import _default_cls_attr
from apps.verification.constants import SMS_CODE_EXPIRE
from celery_tasks.main import app
from libs.ronglianyun.sms import send_message


# 创建任务，并要用celery实例的task方法装饰
@app.task(bind=True, default_retry_delay=5)
def send_sms_code(self, mobile, sms_code):
    try:
        # 发送短信，用1号短信模板
        result = send_message(mobile, sms_code, int(SMS_CODE_EXPIRE/60), 1)
    except Exception as exc:
        # 发送失败，重试
        raise self.retry(exc=exc)

    # 判断短信是否发送成功，失败就重发
    if result["statusCode"] != "000000":
        raise self.retry(exc='重发')
